package com.zm.exam.springbootexam.interceptor;

import com.zm.exam.springbootexam.entry.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User myinfo = (User) session.getAttribute("myinfo");
//        System.out.println("myinfo = " + myinfo);
        boolean bool = myinfo != null && "管理员".equals(myinfo.getRole());
        if (bool == false){
            response.sendRedirect("login.html?1");
        }
        session.setAttribute("requestURI",request.getRequestURI());
        return bool;
    }
}
